﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Administration;

namespace Mystery.SharePoint
{
    static class Infrastructure
    {
        public const string Owner = "SPM.Infrastructure";

        public static void ProvisionInfrastructure(this SPWebApplication app)
        {
            if (app == null) throw new ArgumentNullException("app");

            WebConfigModifications webConfigModifications = new WebConfigModifications(app, Owner);
            webConfigModifications.AddExpressionBuilder("SPMResources", typeof(ResourceExpressionBuilder));
            webConfigModifications.AddSafeControl(typeof(Mystery.SharePoint.WebControls.WebTemplatePicker));
            webConfigModifications.Commit();

            if (app.JobDefinitions.GetValue<SiteManagementJob>(SiteManagementJob.JobName) == null)
            {
                SiteManagementJob job = new SiteManagementJob(app);
                job.Update();
            }
        }

        public static void DeprovisionInfrastructure(this SPWebApplication app)
        {
            if (app == null) throw new ArgumentNullException("app");

            SiteManagementJob job = app.JobDefinitions.GetValue<SiteManagementJob>(SiteManagementJob.JobName);

            if (job != null)
            {
                job.Delete();
            }
        }
    }
}
